class Stack {
    constructor () {
      this.count = 0
      this.items = {}
    }
    push (element) {
      this.items[this.count] = element
      this.count++
    }
    size () {
      return this.count
    }
    isEmpty () {
      return this.count === 0
    }
    pop () {
      if (this.isEmpty()) {
        return undefined
      }
      const result = this.items[this.count--]
      delete this.items[this.count]
      return result
    }
    peek () {
      return this.items[this.count - 1]
    }
    clear () {
      this.count = 0
      this.items = {}
    }
    toString () {
      if (this.isEmpty()) {
        return ''
      }
      let result = this.items['0']
      for(let i = 1; i < this.count; i++) {
        result = `${result},${this.items[i]}`
      }
      return result
    }
}

function baseConverter (decNumber, base) {
    if (base < 2 || base > 36) {
      return ''
    }
    const stack = new Stack()
    const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    let number = decNumber
    let rem
    let baseString = ''
    while (number > 0) {
      rem = Math.floor(number % base)
      number = Math.floor(number / base)
      stack.push(rem)
    }
    while (!stack.isEmpty()) {
      baseString += digits[stack.pop()]
    }
    return baseString
}
console.log(baseConverter(100, 2))  // 1100100
console.log(baseConverter(100, 8))  // 144
console.log(baseConverter(100, 16)) // 64
console.log(baseConverter(100, 32)) // 34


// 代码分析：
// 在将十进制转换为二进制时，余数为0或1;
// 在将十进制转换八进制时，余数为0~7；
// 在将十进制转换为十六进制时，余数为0~9,A(10),B(11),C(12),D(13),E(14),F(15)。
// 其它依次类推。
